package com.hyts.bmsp.listener;


import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import com.hyts.bmsp.jfinal.beans.WebMappingBean;
import com.hyts.bmsp.jfinal.beans.WebRouteBean;
import com.hyts.bmsp.jfinal.scanner01.JfinalClassScanner;
import com.hyts.bmsp.interceptor.SessionInterceptor;
import com.jfinal.kit.PropKit;
import org.apache.log4j.Logger;

/**
 * @ClassName: SystemInitalListener
 * @Description: 用于加载系统数据信息初始化操作：<br>
 * 				  系统加载对应初始化：加载系统级别数据信息<br>
 * @author: 李博/Alex
 * @date: 2017-1-9 下午09:19:22
 */
public class SystemInitalListener implements ServletContextListener {

	/**
	 * @fieldName: logger
	 * @fieldType: Logger
	 * @Description: 日志输出器
	 */
	//private static final Logger logger = LoggerFactory.getLogger(SystemInitalListener.class);
	
	private static final Logger logger = Logger.getLogger(SystemInitalListener.class);
	
	public static String FULL_CONTEXT_PATH = null;
	
	/* (non Javadoc)
	 * @Title: contextDestroyed
	 * @Description: TODO
	 * @param arg0
	 * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
	 */
	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		// TODO Auto-generated method stub

	}

	/* (non Javadoc)
	 * @Title: contextInitialized
	 * @Description: 系统启动上下文时候：进行初始化Jfinal框架的数据Route映射以及Mapping映射关系
	 * @param arg0
	 * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void contextInitialized(ServletContextEvent arg0) {

		FULL_CONTEXT_PATH = arg0.getServletContext().getRealPath("/").endsWith("\\")?arg0.getServletContext().getRealPath("\\"):arg0.getServletContext().getRealPath("/")+"\\";
	    logger.info("初始化所有系统Route路由关系映射集合操作-加载系统所有相关配置WebRoute注解的class类......");

	    JfinalClassScanner.doScanner(PropKit.use("web.properties").get("webRoutePackage", "com"));

	    for (WebRouteBean bean : JfinalClassScanner.getRoutesList())
	    {
	      logger.info("Jfinal-Route路由类名:" + bean.getClassEntry().getClass().getName() + " -> RouteKey（路由键）:" + bean.getRouteKey() + " ->  RouteValue（路由值）:" + bean.getRouteValue());
	    }

	    logger.info("初始化所有系统Route路由关系映射集合操作结束");

	    logger.info("初始化所有系统Mapping路由关系映射集合操作-加载系统所有相关配置WebMapping注解的class类......");

	    JfinalClassScanner.doScanner(PropKit.use("web.properties").get("webMappingPackage", "com"));
	    for (WebMappingBean bean : JfinalClassScanner.getMappingsList())
	    {
	      logger.info("Jfinal-Mapping-Model类名:" + bean.getModelClass().getName() + " -> （表名）:" + bean.getTableName() + " ->  主键:" + bean.getPrimaryKey());
	    }
	    logger.info("初始化所有系统Mapping路由关系映射集合操作结束");

	    String excludeUrls = "/,/index,/doLogin";
	    SessionInterceptor.EXCLUDE_URI_PATH = excludeUrls.split(",");
	}

}
